<template>
  <van-button :loading="loading" :icon="value===1? 'good-job': 'good-job-o'" :class="{ liked:value===1 }" @click="onCollect" />
</template>

<script>
import { addLike, deleteLike } from '@/api/article.js'
export default {
  name: 'LikeArticle',
  data() {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [String, Number, Object],
      required: true
    }
  },
  methods: {
    async onCollect() {
      this.loading = true
      try {
        let status = -1
        if (this.value === 1) {
          // 已经点赞 对点赞进行取消
          await deleteLike(this.articleId)
          this.$toast.success('取消点赞成功')
        } else {
          // 没有点赞 对文章进行点赞
          await addLike(this.articleId)
          this.$toast.success('点赞成功')
          status = 1
        }
        // 更新视图
        // this.$emit('inout', !this.value)
        this.$emit('input', status)
      } catch (err) {
        this.$toast.fail('点赞失败')
      }
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.liked {
  .van-icon {
    color: #e5645f;
  }
}
</style>
